package org.dht.com.tra.nw;

import org.apache.log4j.Logger;
import org.dht.com.tra.Communicator;

/**
 * The <code>CommunicationManagerUDP</code> class is an
 * <code>CommunicationManagerNetworkLAN</code>. Implemented the creation of
 * unicast manager. Required params: PORT_UDP and BUFFER_SIZE_UDP
 */
public class CommunicationManagerUDP extends CommunicationManagerNetworkLAN {

	public enum CommunicationManagerUDPProperties {
		PORT_UDP, BUFFER_SIZE_UDP
	}

	/**
	 * Logger
	 */
	private static final Logger logger = Logger
			.getLogger(CommunicationManagerUDP.class);

	/**
	 * Not implemented
	 */
	protected Communicator createUnicastBigManager() {
		return null;
	}

	/**
	 * Creates a UnicastManagerUDP instance. Required param in
	 * CommunicationProperties called PORT_UDP and BUFFER_SIZE_UDP
	 */
	protected Communicator createUnicastManager() {
		int portUdp;
		long bufferUdp;

		if (communicationProperties
				.containsKey(CommunicationManagerUDPProperties.PORT_UDP.name())) {
			portUdp = Integer.parseInt(communicationProperties
					.get(CommunicationManagerUDPProperties.PORT_UDP.name()));
		} else {
			IllegalArgumentException illegalArgumentException = new IllegalArgumentException(
					"Property PORT_UDP not found");

			logger.error("Property PORT_UDP not found",
					illegalArgumentException);

			throw illegalArgumentException;
		}

		if (communicationProperties
				.containsKey(CommunicationManagerUDPProperties.BUFFER_SIZE_UDP
						.name())) {
			bufferUdp = Long.parseLong(communicationProperties
					.get(CommunicationManagerUDPProperties.BUFFER_SIZE_UDP
							.name()));
		} else {
			IllegalArgumentException illegalArgumentException = new IllegalArgumentException(
					"Property BUFFER_SIZE_UDP not found");

			logger.error("Property BUFFER_SIZE_UDP not found",
					illegalArgumentException);

			throw illegalArgumentException;
		}
		unicastManager = new UnicastManagerUDP(portUdp, bufferUdp);

		return unicastManager;
	}

}
